package com.example.photograph.controller;

import com.example.photograph.domain.AjaxResult;
import com.example.photograph.domain.SysUserDep;
import com.example.photograph.domain.Ztree;
import com.example.photograph.page.TableDataInfo;
import com.example.photograph.service.ISysUserDepService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 用户和部门关联Controller
 * 
 * @author wuyj
 * @date 2021-03-06
 */
@Controller
@RequestMapping("/photograph/dep")
public class SysUserDepController extends BaseController
{
    private String prefix = "photograph/dep";

    @Autowired
    private ISysUserDepService sysUserDepService;

    @RequiresPermissions("photograph:dep:view")
    @GetMapping()
    public String dep()
    {
        return prefix + "/dep";
    }

    /**
     * 查询用户和部门关联列表
     */
    @RequiresPermissions("photograph:dep:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysUserDep sysUserDep)
    {
        startPage();
        List<SysUserDep> list = sysUserDepService.selectSysUserDepList(sysUserDep);
        return getDataTable(list);
    }

    /**
     * 新增用户和部门关联
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用户和部门关联
     */
    @RequiresPermissions("photograph:dep:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysUserDep sysUserDep)
    {
        return toAjax(sysUserDepService.insertSysUserDep(sysUserDep));
    }

    /**
     * 修改用户和部门关联
     */
    @GetMapping("/edit/{userId}")
    public String edit(@PathVariable("userId") Long userId, ModelMap mmap)
    {
        SysUserDep sysUserDep = sysUserDepService.selectSysUserDepById(userId);
        mmap.put("sysUserDep", sysUserDep);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户和部门关联
     */
    @RequiresPermissions("photograph:dep:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysUserDep sysUserDep)
    {
        return toAjax(sysUserDepService.updateSysUserDep(sysUserDep));
    }

    /**
     * 删除用户和部门关联
     */
    @RequiresPermissions("photograph:dep:remove")
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysUserDepService.deleteSysUserDepByIds(ids));
    }

    /**
     * 加载员工列表树
     */
    @GetMapping("/userDepTreeData")
    @ResponseBody
    public List<Ztree> userDepTreeData(Long orderId)
    {
        List<Ztree> ztrees = sysUserDepService.userDepTreeData(orderId);
        return ztrees;
    }
}
